﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.FileManipulation
{
    public partial class FtpEditor : Form, IComponentEditor 
    {
        private object _OwnerActivityNode;
        private FtpComponent _Ftp;

        /// <summary>
        /// 
        /// </summary>
        public FtpEditor()
        {
            InitializeComponent();
        }


        public event UpdateComponentSettings UpdateComponentSettingEvent;

        public object OwnerActivityNode
        {
            get { return _OwnerActivityNode; }
            set { _OwnerActivityNode = value; }
        }

        public Type WorkflowComponentType
        {
            get { return typeof(FtpComponent); }
        }

        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents,
                                 object activityNode, WorkflowComponentBase wfComponent)
        {
            this._OwnerActivityNode = activityNode;
            this._Ftp = (FtpComponent) wfComponent;

            this.cbo_Protocol.SelectedIndex = (int) this._Ftp.Protocol;
            this.txt_Host.Text = this._Ftp.FtpSite;
            this.txt_UserName.Text = this._Ftp.UserName;
            this.txt_Password.Text = this._Ftp.Password;
            this.rd_Download.Checked = true;
            if(this._Ftp.Direction==FtpDirection.Upload)
                this.rd_Upload.Checked = true;
            this.ck_Anonymous.Checked = this._Ftp.Anonymous;
            this.ck_WindowsAuth.Checked = this._Ftp.WindowsAuth;
            this.txt_SrcPath.Text = this._Ftp.SrcPath;
            this.txt_TgtFolder.Text = this._Ftp.TgtFolder;
        }

        public void SaveSettings()
        {
            this._Ftp.FtpSite = this.txt_Host.Text;
            this._Ftp.Protocol = (FtpProtocol) this.cbo_Protocol.SelectedIndex;
            this._Ftp.Direction = FtpDirection.Download;
            if(this.rd_Upload.Checked)
                this._Ftp.Direction = FtpDirection.Upload;
            this._Ftp.Anonymous = this.ck_Anonymous.Checked;
            this._Ftp.WindowsAuth = this.ck_WindowsAuth.Checked;
            this._Ftp.UserName = this.txt_UserName.Text;
            this._Ftp.Password = this.txt_Password.Text;
            this._Ftp.SrcPath = this.txt_SrcPath.Text;
            this._Ftp.TgtFolder = this.txt_TgtFolder.Text;
        }

        public TestResult Test()
        {
            this.SaveSettings();
            FtpInstruction instruction = new FtpInstruction(this._Ftp.ComponentID, Guid.NewGuid(),
                this._Ftp.FtpSite, this._Ftp.Protocol, this._Ftp.Direction,
                this._Ftp.SrcPath, this._Ftp.TgtFolder, this._Ftp.UserName,
                this._Ftp.Password, this._Ftp.Anonymous, this._Ftp.WindowsAuth);
            FtpWorker worker = new FtpWorker();
            worker.SetInstruction(Guid.NewGuid(), instruction, string.Empty, this._Ftp.ComponentName);
            try
            {
                worker.DoWork();
                if(this._Ftp.Direction==FtpDirection.Download)
                {
                    if(File.Exists(((FtpOutput)worker.Output).TgtPath))
                    {
                        return new TestResult(true,"Download successful!");
                    }
                    else
                    {
                        return new TestResult(false, "Failed to download");
                    }
                }
                else
                {
                    return new TestResult(true, "Upload successful!");
                }
            }
            catch (Exception ex)
            {
                return new TestResult(false,ex.ToString());
            }
        }

        private void ck_Anonymous_CheckedChanged(object sender, EventArgs e)
        {
            if(this.ck_Anonymous.Checked)
            {
                this.txt_UserName.Enabled = false;
                this.txt_Password.Enabled = false;
            }
            else
            {
                this.txt_UserName.Enabled = true;
                this.txt_Password.Enabled = true;
            }
        }

        private void ck_WindowsAuth_CheckedChanged(object sender, EventArgs e)
        {
            if (this.ck_WindowsAuth.Checked)
            {
                this.txt_UserName.Enabled = false;
                this.txt_Password.Enabled = false;
            }
            else
            {
                this.txt_UserName.Enabled = true;
                this.txt_Password.Enabled = true;
            }
        }

        private void btn_Browse1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg=new OpenFileDialog();
            if(dlg.ShowDialog()==DialogResult.OK)
            {
                this.txt_SrcPath.Text = dlg.FileName;
            }
        }

        private void btn_Browse2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg=new FolderBrowserDialog();
            if(dlg.ShowDialog()==DialogResult.OK)
            {
                this.txt_TgtFolder.Text = dlg.SelectedPath;
            }
        }

        private void rd_Download_CheckedChanged(object sender, EventArgs e)
        {
            if(this.rd_Download.Checked)
                this.btn_Browse2.Enabled = true;
            else
                this.btn_Browse2.Enabled = false;
        }

        private void rd_Upload_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rd_Download.Checked)
                this.btn_Browse2.Enabled = true;
            else
                this.btn_Browse2.Enabled = false;
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if (this.UpdateComponentSettingEvent != null)
                this.UpdateComponentSettingEvent(this._OwnerActivityNode, this._Ftp);
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_Test_Click(object sender, EventArgs e)
        {
            this.Test();
        }
    }
}
